const Koa = require('koa')
const Route = require('koa-router')
const mockList = require('./mock')

// 实例化一个服务
const app = new Koa()
const router = new Route()

// 模拟网络加载
async function getRes(res ,ctx){
    return new Promise(resolve => {
        setTimeout(() => {
            let val = res(ctx)
            resolve(val) // promise 方法中想要在执行完后返回的值 用resolve方法返回，理解为return
        },2000)
    })
}

mockList.forEach(item => {
    const {url , method , response} = item 
    router[method](url , async ctx=> { // ctx contextcontent :环境上下文的缩写
        const res = await getRes(response ,ctx)
        // const res = response()
        ctx.body = res
    })
})

app.use(router.routes())
app.listen(8080)